define(function (require, exports, module) {
    require('authorize');
    require('permission');
    var $ = require('jquery');
    var util = require('util');
    var ui = require('ui');
    var angular = require("angular");
    require("angular-interceptor");
    var dialog = require("dialog");
    require("angular-ui");
    require("angular-messages");
    require("jqtree");
    require("/public/js/service/public_no/service");
    require("/static_public/js/angular_directive/clipboard");
    require('global');

    var api_prefix = G_CONFIG["api_prefix"]; //api前缀

    angular.module('setPublic', ['ngMessages', 'ngUI', 'ngInterceptor', 'angular.directive.clipboard', 'publicNoService'])
        .controller('GzhAddInfoController', ['$scope', '$http', 'commonService',
            function ($scope, $http, commonService) {
                function fixedData(data) {
                    angular.forEach(data, function (item) {
                        item.children = item.childNodes;
                        fixedData(item.childNodes);
                    });
                    return data;
                }

                //搜索
                $scope.winXinType = {
                    typeList: [
                        {value: '1', text: '订阅号'},
                        {value: '2', text: '微信老帐号升级后的订阅号'},
                        {value: '3', text: '服务号'}
                    ]
                };

                var $tree = $("#tree_table");
                $scope.isTreeCollapse = false;

                $scope.toggleTreeCombo = function () {
                    $scope.isTreeCollapse = !$scope.isTreeCollapse;
                    var clickWindow = function (event) {
                        var target = $(event.target);
                        if ($tree.parent().has(target).length < 1) {
                            if ($scope.isTreeCollapse) {
                                $scope.isTreeCollapse = false;
                                $scope.$apply();

                                $(window).off('click', clickWindow);
                            }
                        }
                    };
                    $(window).on('click', clickWindow);
                };

                $scope.cancel = function () {
                    location.href = '/';
                };

                var getOrgByGuid = function (buGuid, orgs) {
                    buGuid && (buGuid = buGuid.toUpperCase());

                    var item;
                    for (var i = 0; item = orgs[i]; i++) {
                        var id = item.id;
                        if (id) {
                            id = id.toUpperCase();
                        }

                        if (id === buGuid) {
                            return item;
                        } else if (item.children) {
                            var orgByGuid = getOrgByGuid(buGuid, item.children);
                            if (orgByGuid) {
                                return orgByGuid;
                            }
                        }
                    }
                    return null;
                };

                $scope.save = function () {
                    var data = $scope.data;
                    commonService.saveInEdit({
                        buguid: $scope.belongsOrganization.id,
                        wxname: data.wxname,
                        wxid: data.wxid,
                        weixin: data.weixin,
                        winxintype: data.winxintype,
                        appid: data.appid,
                        appsecret: data.appsecret,
                        is_third_auth: data.is_third_auth
                    })
                        .then(function (res) {
                            if (res.data.retCode == 0) {
                                ui.showSucTip();
                            }
                        });
                };

                //所属组织
                commonService.queryAllOrganizations().then(function (res) {
                    if (res && res.data && res.data.retCode == 0) {

                        var orgs = fixedData(res.data.data);
                        $scope.orgs = orgs;

                        $tree.tree({
                            data: orgs,
                            dragAndDrop: false,
                            useContextMenu: false,
                            autoOpen: 0, //展开级别
                            onCreateLi: function (node, $li) {
                            },
                            //是否可以移动
                            onCanMove: function (node) {
                                return false;
                            },
                            //是否可以移动到
                            onCanMoveTo: function (moved_node, target_node, position) {
                                //只能在同级节点中移动
                                return false;
                            },
                            //移动句柄
                            onIsMoveHandle: function ($ele) {
                                return false;
                            }
                        }).on(
                            'tree.click',
                            function (event) {
                                var node = event.node;
                                $scope.belongsOrganization = node;
                                $scope.isTreeCollapse = false;
                                $scope.$apply();
                            }
                        );
                    }

                    commonService.getAccount().then(function (res) {
                        var retCode = res.data.retCode;
                        var data = res.data.data;

                        if (retCode == 0) {
                            var baseinfo = data.baseinfo;

                            $scope.data = baseinfo;
                            $scope.stepTwoData = data;
                            $scope.stepThreeData = data.application;

                            $scope.belongsOrganization = getOrgByGuid(baseinfo.buguid, $scope.orgs);
                        }
                    });
                });
            }])

    angular.bootstrap(document, ['setPublic']);
});